﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Attribute = netDxf.Entities.Attribute;
using Image = netDxf.Entities.Image;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;

namespace TestDxfDocument
{
    class class_ISYC_303_05
    {
        public static void ISYC_303_05(Dictionary<string, Object> data) //直柄T型槽立铣刀
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中性线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层

            //作图
            //参数
            string path = Convert.ToString(data["path"]);
            double soal = Convert.ToDouble(data["soal"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            //double scale = 1; //放大系数
            if (scale == 0 && soal < 67)
            {
                scale = 4.0;
            }
            else if (scale == 0 && soal < 90)
            {
                scale = 3.0;
            }
            else if (scale == 0 && soal < 134)
            {
                scale = 2.0;
            }
            else if (scale == 0 && soal < 178)
            {
                scale = 1.5;
            }
            else if (scale == 0 && soal < 268)
            {
                scale = 1.0;
            }
            else if (scale == 0 && soal < 357)
            {
                scale = 0.75;
            }
            else if (scale == 0)
            {
                scale = 0.5;
            }
            double pi = Math.PI;
            double dc = Convert.ToDouble(data["dc"]) * scale;
            double dconms = Convert.ToDouble(data["dconms"]) * scale;
            double oal = Convert.ToDouble(data["oal"]) * scale;
            double ls = Convert.ToDouble(data["ls"]) * scale;
            double apmx = Convert.ToDouble(data["apmx"]) * scale;
            double dn = Convert.ToDouble(data["dn"]) * scale;
            double lh = oal - ls - (dconms - dn) / 2;
            double isframe = Convert.ToDouble(data["isframe"]);

            /*double dc = 60 * scale;
            double dconms = 10 * scale;
            double oal = 120 * scale;
            double ls = 35 * scale;
            double apmx = 20 * scale;
            double dn = 8 * scale;
            double lh = oal - ls - (dconms - dn) / 2;
            double isframe = 0;*/


            //坐标
            //主视图
            Vector2 v1 = new Vector2(dconms / 2 - 0.5, 0);
            Vector2 v2 = new Vector2(v1.X + 0.5, -0.5);
            Vector2 v3 = new Vector2(v2.X, -ls);
            Vector2 v4 = new Vector2(dn / 2, v3.Y - (dconms - dn) / 2 * Math.Cos(45 * pi / 180) / Math.Sin(45 * pi / 180));
            Vector2 v5 = new Vector2(v4.X, -oal + apmx + (dconms - dn) / 2 * Math.Cos(45 * pi / 180) / Math.Sin(45 * pi / 180));
            Vector2 v6 = new Vector2(v3.X, -oal + apmx);
            Vector2 v8 = new Vector2(dc / 2, v6.Y);
            Vector2 v7 = new Vector2((v8.X + v6.X) / 2, v6.Y);
            Vector2 v9 = new Vector2(dc / 2, -oal);
            Vector2 v10 = new Vector2(dc / 4, -oal);
            Vector2 v11 = new Vector2(-v10.X, v10.Y);
            Vector2 v12 = new Vector2(-v9.X, v9.Y);
            Vector2 v13 = new Vector2(-v8.X, v8.Y);
            Vector2 v14 = new Vector2(-v7.X, v7.Y);
            Vector2 v15 = new Vector2(-v6.X, v6.Y);
            Vector2 v16 = new Vector2(-v5.X, v5.Y);
            Vector2 v17 = new Vector2(-v4.X, v4.Y);
            Vector2 v18 = new Vector2(-v3.X, v3.Y);
            Vector2 v19 = new Vector2(-v2.X, v2.Y);
            Vector2 v20 = new Vector2(-v1.X, v1.Y);
            Vector2 v21 = new Vector2(0, 2);
            Vector2 v22 = new Vector2(0, -oal - 2);

            //线段
            //主视图
            Vector2[] Line1 = { v1, v2 };
            fun.drawBLine(dxf, Line1, 0.3, contour);
            Vector2[] Line2 = { v2, v3 };
            fun.drawBLine(dxf, Line2, 0.3, contour);
            Vector2[] Line3 = { v3, v4 };
            fun.drawBLine(dxf, Line3, 0.3, contour);
            Vector2[] Line4 = { v4, v5 };
            fun.drawBLine(dxf, Line4, 0.3, contour);
            Vector2[] Line5 = { v5, v6 };
            fun.drawBLine(dxf, Line5, 0.3, contour);
            Vector2[] Line6 = { v6, v7 };
            fun.drawBLine(dxf, Line6, 0.3, contour);
            Vector2[] Line7 = { v7, v8 };
            fun.drawBLine(dxf, Line7, 0.5, contour);
            Vector2[] Line8 = { v8, v9 };
            fun.drawBLine(dxf, Line8, 0.5, contour);
            Vector2[] Line9 = { v9, v10 };
            fun.drawBLine(dxf, Line9, 0.5, contour);
            Vector2[] Line10 = { v10, v11 };
            fun.drawBLine(dxf, Line10, 0.3, contour);
            Vector2[] Line11 = { v11, v12 };
            fun.drawBLine(dxf, Line11, 0.5, contour);
            Vector2[] Line12 = { v12, v13 };
            fun.drawBLine(dxf, Line12, 0.5, contour);
            Vector2[] Line13 = { v13, v14 };
            fun.drawBLine(dxf, Line13, 0.5, contour);
            Vector2[] Line14 = { v14, v15 };
            fun.drawBLine(dxf, Line14, 0.3, contour);
            Vector2[] Line15 = { v15, v16 };
            fun.drawBLine(dxf, Line15, 0.3, contour);
            Vector2[] Line16 = { v16, v17 };
            fun.drawBLine(dxf, Line16, 0.3, contour);
            Vector2[] Line17 = { v17, v18 };
            fun.drawBLine(dxf, Line17, 0.3, contour);
            Vector2[] Line18 = { v18, v19 };
            fun.drawBLine(dxf, Line18, 0.3, contour);
            Vector2[] Line19 = { v19, v20 };
            fun.drawBLine(dxf, Line19, 0.3, contour);
            Vector2[] Line20 = { v20, v1 };
            fun.drawBLine(dxf, Line20, 0.3, contour);
            Vector2[] Line21 = { v21, v22 };
            fun.drawPlinetype(dxf, Line21, 0.15, centre, Linetype.Center);
            Vector2[] Line22 = { v19, v2 };
            fun.drawBLine(dxf, Line22, 0.3, contour);
            Vector2[] Line23 = { v18, v3 };
            fun.drawBLine(dxf, Line23, 0.3, contour);
            Vector2[] Line24 = { v17, v4 };
            fun.drawBLine(dxf, Line24, 0.3, contour);
            Vector2[] Line25 = { v16, v5 };
            fun.drawBLine(dxf, Line25, 0.3, contour);
            Vector2[] Line26 = { v15, v6 };
            fun.drawBLine(dxf, Line26, 0.3, contour);

            //标注样式
            //箭头样式
            DimensionStyle dimensionStyle = fun.CreateDimStyle();
            dimensionStyle.ArrowSize = 0.2;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;

            //文本样式
            dimensionStyle.TextHeight = 2.0;
            double offset = 10;

            //线段尺寸标注
            //尺寸1
            LinearDimension dim1 = new LinearDimension(new Line(v4, v9), offset * 2 + (v9.X - v4.X) * 1.5, 90, dimensionStyle);
            dim1.UserText = "LH" /*+ Math.Round(lh / scale, 2)*/;
            dxf.AddEntity(dim1);
            //尺寸2
            LinearDimension dim2 = new LinearDimension(new Line(v12, v9), -offset, 0, dimensionStyle);
            dim2.UserText = "DC" /*+ Math.Round(dc / scale, 2)*/;
            dxf.AddEntity(dim2);
            //尺寸3
            LinearDimension dim3 = new LinearDimension(new Line(v8, v9), offset, 270, dimensionStyle);
            dim3.UserText = "APMX" /*+ Math.Round(apmx / scale, 2)*/;
            dxf.AddEntity(dim3);
            //尺寸4
            LinearDimension dim4 = new LinearDimension(new Line(v1, v3), offset + 0.25 + v8.X - v3.X, 270, dimensionStyle);
            dim4.UserText = "LS" /*+ Math.Round(ls / scale, 2)*/;
            dxf.AddEntity(dim4);
            //尺寸5
            LinearDimension dim5 = new LinearDimension(new Line(v9, v1), offset * 3 + 0.25 + v9.X - (v3.X + v9.X) / 2, 270, dimensionStyle);
            dim5.UserText = "OAL" /*+ Math.Round(oal / scale, 2)*/;
            dxf.AddEntity(dim5);
            //尺寸6
            LinearDimension dim6 = new LinearDimension(new Line(v19, v2), offset + 0.5, 0, dimensionStyle);
            dim6.UserText = "DCONMS" /*+ Math.Round(dconms / scale, 2)*/;
            dxf.AddEntity(dim6);
            //尺寸7
            LinearDimension dim7 = new LinearDimension(new Line(v17, v4), -(v4.Y - v5.Y) / 2, 0, dimensionStyle);
            dim7.UserText = "DN" /*+ Math.Round(dn / scale, 2)*/;
            dxf.AddEntity(dim7);

            //图框
            if (isframe == 1)
            {
                Dictionary<string, Vector2> position = Frame.frameA3Y(dxf, new Vector2(-148.5, 210 - oal / 2));
                fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
            }
            //定义格式，根据路径导出
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }
    }
}
